package de.fhin.mas.agents.manager.behaviours;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import de.fhin.mas.MASEvents;
import de.fhin.mas.agents.manager.ManagerAgent;

/**
 * Handling von system_stop_required eines anderen Ageten.
 * 
 * @author Ast
 */
public class HandleSystemStop extends CyclicBehaviour {
	private static final long serialVersionUID = 1L;
	private final ManagerAgent managerAgent;

	public HandleSystemStop(ManagerAgent a) {
		super(a);
		managerAgent = a;
	}

	@Override
	public void action() {
		MessageTemplate mt = MessageTemplate.and(MessageTemplate.MatchConversationId(MASEvents.SYSTEM_STOP_REQUIRED
				.getName()), MessageTemplate.MatchPerformative(ACLMessage.REQUEST));

		ACLMessage msg = myAgent.receive(mt);
		if (msg == null)
			block();
		else {
			// Info an den User
			managerAgent.sendMsgToUser("REQUEST: SYSTEM_STOP_REQUIRED");
			managerAgent.sendMsgToUser("MESSAGE: " + msg.getContent());

			// Reply schicken.
			// Da der Manager keine Aktoren hat brauchen wir hier nicht mehr zu
			// machen.
			ACLMessage agree = msg.createReply();
			agree.setPerformative(ACLMessage.AGREE);
			managerAgent.sendToAll(agree);

			managerAgent.logInfo("Agree SystemStop was sent");
		}
	}

}
